//
//  HomeYunShuMapCell.m
//  ReceiptDelivery
//
//  Created by 吕晶 on 2020/11/17.
//  Copyright © 2020 吕晶. All rights reserved.
//

#import "HomeYunShuMapCell.h"
#import <MapKit/MapKit.h>
#import "HomeAnnotation.h"
#import "HomeAnnotationView.h"

@interface HomeYunShuMapCell ()<MKMapViewDelegate>

@property (nonatomic, strong) UIView *bgView;
@property (nonatomic, strong) UILabel *countLab;
@property (nonatomic, strong) UILabel *titleLab;
@property (nonatomic, strong) UIImageView *arrowImgView;
@property (nonatomic, strong) MKMapView *mapView;

@end

@implementation HomeYunShuMapCell

+ (instancetype)HomeYunShuMapCellWithTableView:(UITableView *)tableView {
    static NSString *cellID = @"HomeYunShuMapCell";
    HomeYunShuMapCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID];
    if (cell == nil) {
        cell = [[HomeYunShuMapCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellID];
    }
    return cell;
}

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        self.backgroundColor = Color_backgroundColor;
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        
        self.bgView = [[UIView alloc] initWithFrame:CGRectMake(10, 0, ScreenW - 20, 287)];
        self.bgView.backgroundColor = HexColor(0xffffff);
        self.bgView.layer.masksToBounds = YES;
        self.bgView.layer.cornerRadius = 6;
        [self.contentView addSubview:self.bgView];
        
        self.height = self.bgView.nim_bottom;
        
        self.mapView = [[MKMapView alloc]initWithFrame:CGRectMake(5, self.bgView.nim_height - 5 - 240, self.bgView.nim_width - 10, 240)];
        self.mapView.delegate = self;
        [self.bgView addSubview:self.mapView];
        
        self.arrowImgView = [[UIImageView alloc] initWithFrame:CGRectMake(self.bgView.nim_width - 8 - 18, (self.mapView.nim_top - 18) * 0.5, 18, 18)];
        self.arrowImgView.image = [UIImage imageNamed:@"icon_home_arrow"];
        [self.bgView addSubview:self.arrowImgView];
        
        self.countLab = [UILabel new];
        self.countLab.textColor = HexColor(0xFBA962);
        self.countLab.font = BoldFontOfSize(17);
        [self.bgView addSubview:self.countLab];
        
        self.titleLab = [UILabel new];
        self.titleLab.textColor = HexColor(0x2B2C44);
        self.titleLab.font = BoldFontOfSize(17);
        [self.bgView addSubview:self.titleLab];
    }
    return self;
}

-(void)setModel:(HomeYunShuMapModel *)model {
    _model = model;
    [self configUI];
}

- (void)configUI {
    self.countLab.text = [NSString stringWithFormat:@"%d",self.model.intransit_num];
    [self.countLab sizeToFit];
    self.countLab.nim_left = 5;
    self.countLab.nim_centerY = self.arrowImgView.nim_centerY;
    
    self.titleLab.text = @"个任务在运输中";
    [self.titleLab sizeToFit];
    self.titleLab.nim_left = self.countLab.nim_right + 5;
    self.titleLab.nim_centerY = self.countLab.nim_centerY;
    
    __block NSMutableArray *quYuArray = [NSMutableArray array];
    [self.model.location enumerateObjectsUsingBlock:^(HomeYunShuMapDotModel * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        HomeAnnotation *annotation = [HomeAnnotation new];
        annotation.coordinate = CLLocationCoordinate2DMake([obj.lat doubleValue], [obj.lng doubleValue]);
        [self.mapView addAnnotation:annotation];
        [quYuArray addObject:annotation];
    }];
    if (!self.model.location.count) {
        [self.mapView removeAnnotations:[self.mapView annotations]];
    }else {
        self.mapView.region = [HomeAnnotation regionForAnnotations:quYuArray];
    }
}

- (nullable MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation{
    // 自定义大头针
    if ([annotation isKindOfClass:[HomeAnnotation class]]) {
        static NSString *pinID = @"HomeAnnotation";
        HomeAnnotationView *customPinView = (HomeAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:pinID];
        if (!customPinView) {
           customPinView = [[HomeAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:pinID];
        }
        customPinView.annotation = (HomeAnnotation *)annotation;
        return customPinView;
    }
    return nil;
}

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
